package com.dweb.myjsdemo;

import com.dweb.myjsdemo.widget.controller.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Operation;
import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

public class MainAbility extends AceAbility {
  public static final int DEFAULT_DIMENSION_2X2 = 2;
  public static final int DIMENSION_1X2 = 1;
  public static final int DIMENSION_2X4 = 3;
  public static final int DIMENSION_4X4 = 4;
  private static final int INVALID_FORM_ID = -1;
  private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, MainAbility.class.getName());
  private String topWidgetSlice;

  @Override
  public void onStart(Intent intent) {
    super.onStart(intent);
    if (intentFromWidget(intent)) {
      topWidgetSlice = getRoutePageSlice(intent);
      if (topWidgetSlice != null) {
        setMainRoute(topWidgetSlice);
      }
    }
    stopAbility(intent);
  }

  @Override
  public void onStop() {
    super.onStop();
  }

  @Override
  protected ProviderFormInfo onCreateForm(Intent intent) {
    HiLog.info(TAG, "onCreateForm");
    long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
    String formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
    int dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
    HiLog.info(TAG, "onCreateForm: formId=" + formId + ",formName=" + formName);
    FormControllerManager formControllerManager = FormControllerManager.getInstance(this);
    FormController formController = formControllerManager.getController(formId);
    formController = (formController == null) ? formControllerManager.createFormController(formId,
            formName, dimension) : formController;
    if (formController == null) {
      HiLog.error(TAG, "Get null controller. formId: " + formId + ", formName: " + formName);
      return null;
    }
    return formController.bindFormData();
  }

  @Override
  protected void onUpdateForm(long formId) {
    HiLog.info(TAG, "onUpdateForm");
    super.onUpdateForm(formId);
    FormControllerManager formControllerManager = FormControllerManager.getInstance(this);
    FormController formController = formControllerManager.getController(formId);
    formController.updateFormData(formId);
  }

  @Override
  protected void onDeleteForm(long formId) {
    HiLog.info(TAG, "onDeleteForm: formId=" + formId);
    super.onDeleteForm(formId);
    FormControllerManager formControllerManager = FormControllerManager.getInstance(this);
    formControllerManager.deleteFormController(formId);
  }

  @Override
  protected void onTriggerFormEvent(long formId, String message) {
    super.onTriggerFormEvent(formId, message);

    System.out.println(message);

    //启动ServiceAbility 并通过intent传入formId
    Intent intent = new Intent();
    Operation operation = new Intent.OperationBuilder()
            .withBundleName("com.dweb.myjsdemo")
            .withAbilityName("com.dweb.myjsdemo.ServiceAbility")
            .build();
    intent.setOperation(operation);
    intent.setParam("formId",formId);
    startAbility(intent);
    //-------

    ZSONObject params = ZSONObject.stringToZSON(message);
    String content = params.getString("itemContent");
    System.out.println(content);
    FormBindingData bindingData = new FormBindingData(params);

    try {
      updateForm(formId,bindingData);
    } catch (FormException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void onNewIntent(Intent intent) {
    if (intentFromWidget(intent)) { // Only response to it when starting from a service widget.
      String newWidgetSlice = getRoutePageSlice(intent);
      if (topWidgetSlice == null || !topWidgetSlice.equals(newWidgetSlice)) {
        topWidgetSlice = newWidgetSlice;
        restart();
      }
    }
  }

  private boolean intentFromWidget(Intent intent) {
    long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
    return formId != INVALID_FORM_ID;
  }

  private String getRoutePageSlice(Intent intent) {
    long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
    if (formId == INVALID_FORM_ID) {
      return null;
    }
    FormControllerManager formControllerManager = FormControllerManager.getInstance(this);
    FormController formController = formControllerManager.getController(formId);
    if (formController == null) {
      return null;
    }
    Class<? extends AbilitySlice> clazz = formController.getRoutePageSlice(intent);
    if (clazz == null) {
      return null;
    }
    return clazz.getName();
  }
}
